$(function () {
    var shopId = getQueryString('shopId');
    var isEdit = shopId?true:false;
    var initUrl ='/shop/getShopInitInfo';
    var registerShopUrl = '/shop/registerShop';
    var shopInfoUrl='/shop/getShopById?shopId='+shopId;
    var editShopUrl='/shop/modifyShop';
    if(!isEdit){
        getShopInitInfo();
    }else{
        getShopInfo();
    }
    function getShopInitInfo() {
        $.getJSON(initUrl,function(data){
           if(data.success){
               var tempHtml = '';
               var tempAreaHtml = '';
               data.shopCategoryList.map(function(item,index){
                   tempHtml +='<option data-id="'+item.shopCategoryId+'">'+item.shopCategoryName+'</option>';
               });
               data.areaList.map(function(item,index){
                  tempAreaHtml +='<option data-id="'+item.areaId+'">'+item.areaName +'</option>';
               });
               $('#shop-category').html(tempHtml);
               $('#area').html(tempAreaHtml);
           }
        });
    }
    function getShopInfo(shopId){
        $.getJSON(shopInfoUrl,function(data){
            if(data.success){
                var shop = data.shopDTO;
                $('#shop-name').val(shop.shopName);
                $('#shop-addr').val(shop.shopAddr);
                $('#shop-phone').val(shop.phone);
                $('#shop-desc').val(shop.shopDesc);
                var shopCategory = '<option data-id="'+shop.shopCategoryId+'"selected>'
                +shop.shopCategoryName+'</option>';
                var tempAreaHtml ='';
                data.areaList.map(function(item,index){
                    tempAreaHtml +='<option data-id="'+item.areaId+'">'+item.areaName +'</option>';
                });
                $('#shop-category').html(shopCategory);
                $('#shop-category').attr('disabled','disabled');
                $('#area').html(tempAreaHtml);
                $("#area option[data-id='"+shop.areaId+"']").attr("selected","selected");
            }
        });
    }

    $("#submit").click(function(){
        var shopName = $('#shop-name').val();
        var shopAddr = $('#shop-addr').val();
        var phone = $('#shop-phone').val();
        var shopDesc = $('#shop-desc').val();
        var shopCategoryId=$('#shop-category').find('option').not(function () {
            return !this.selected
        }).data('id');
        var areaId = $('#area').find('option').not(function(){
            return !this.selected
        }).data('id');
        var file = $("#shop-img")[0].files[0];
        var verifyCodeActual = $('#j_captcha').val();
        if(!verifyCodeActual){
            $.toast("请输入验证码");
            return;
        }
        var formData = new FormData();
        if(file!=null) {
            formData.append("file", file);
        }
        formData.append("shopName",shopName);
        formData.append("shopAddr",shopAddr);
        formData.append("phone",phone);
        formData.append("shopDesc",shopDesc);
        formData.append("shopCategoryId",shopCategoryId);
        formData.append("areaId",areaId);
        formData.append("verifyCodeActual",verifyCodeActual);
        if(shopId!=null){
            formData.append("shopId",shopId);
        }
        $.ajax({
            url:(isEdit?editShopUrl:registerShopUrl),
            enctype: 'multipart/form-data',
            type:'POST',
            data: formData,
            contentType:false,
            processData:false,
            cache:false,
            success:function(data){
                if(data.success){
                    $.toast('提交成功!');
                }else{
                    $.toast('提交失败！'+data.errMsg);
                }
                $('#captcha_img').click();
            }
        })
    });

})